﻿using Inovout.Data.Domain;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading;

namespace Inovout.Data.Repository
{
    public class DataObjectRepository<TDataObject> : BaseRepository<TDataObject>,IRepository<TDataObject> where TDataObject : DataObject
    {
        public override void Save(TDataObject dataObject)
        {
            if (!dataObject.IsTransient)
            {
                dataObject.SetModified(DateTime.Now);
            }
            base.Save(dataObject);
        }

        public void BulkSave(IEnumerable<TDataObject> models)
        {
            if (models.Any())
            {
                using (var tx = this.StatelessSession.BeginTransaction())
                {
                    foreach (var model in models)
                    {
                        if (model.IsTransient)
                        {
                            this.StatelessSession.Insert(model);

                        }
                        else
                        {
                            this.StatelessSession.Update(model);

                        }
                    }
                    tx.Commit();
                }
            }
        }
    }
}
